import { Ref } from 'vue';
import { useEmitter } from 'src/emitter';
import { Condition } from 'src/utils/types';

/**
 * 表格组件查询 composable
 * @param id 表格 id, 可以是 undefined, 但要保证一个页面上只有一个需要查询的表格
 * @param conditions, 查询条件对象
 */
export const useSearch = (id: string, conditions: Ref<Condition>) => {
  useEmitter('table_search', payload => {
    if (payload.id !== id) {
      return;
    }
    if (payload.clause === undefined) {
      if (conditions.value[payload.field]) {
        delete conditions.value[payload.field];
      }
    } else {
      conditions.value[payload.field] = payload.clause;
    }
  });
};
